<?php
namespace PublicNo\Model;
use Think\Model;
class PublicNoModel extends Model {
	protected $connection = 'DB_BASE_DSN';
	protected $tableName = 'qyx_publicno'; 
	
	
	public function searchPublicNoList($data){
		$map = array();
	
		if(!empty($data['searchKey'])){
			$where['qyx_cust.nickname']  = array('like','%'.$data['searchKey'].'%');
			$where['qyx_cust.realname']  = array('like','%'.$data['searchKey'].'%');
			$where['qyx_publicno.accmbody']  = array('like','%'.$data['searchKey'].'%');
			$where['qyx_publicno.fundesc']  = array('like','%'.$data['searchKey'].'%');
			$where['_logic'] = 'or';
			$map['_complex'] = $where;
		}
	
		$map['qyx_cust.status'] = 1;
		$map['qyx_cust.custtype'] = array('in' , array(2,3));
	
		$total = $this->join('join qyx_cust on qyx_publicno.custid = qyx_cust.custid')->where($map)->count();
		$fields = array('qyx_publicno.publicnoid','qyx_cust.custid','qyx_cust.custtype','qyx_cust.nickname','qyx_cust.realname','qyx_cust.pinyinname','qyx_publicno.fundesc','qyx_publicno.accmbody');
	
		if(empty($data['sortField']))$data['sortField'] = "qyx_publicno.publicnoid";
		if(empty($data['sortOrder']))$data['sortOrder'] = "desc";
	
		if(empty($data['pageSize']) && empty($data['pageIndex']))
			$result = $this->join('join qyx_cust on qyx_publicno.custid = qyx_cust.custid')->field($fields)->where($map)->order("{$data['sortField']} {$data['sortOrder']}")->select();
		else
			$result = $this->join('join qyx_cust on qyx_publicno.custid = qyx_cust.custid')->field($fields)->where($map)->order("{$data['sortField']} {$data['sortOrder']}")->limit($data['pageIndex']*$data['pageSize'] , $data['pageSize'])->select();
	
		return array('total'=>$total , 'data'=>$result);
	}
	
	
	public function getPublicNoList($data){
		$map = array();
	
		if(!empty($data['custid'])){
			$map['qyx_friend.custid'] = $data['custid'];
		}
	
		$map['qyx_cust.status'] = 1;
		$map['qyx_cust.custtype'] = array('in' , array(2,3));
	
		$total = $this->join('join qyx_cust on qyx_publicno.custid = qyx_cust.custid')
						->join('join qyx_friend on qyx_publicno.custid = qyx_friend.tcustid')
						->where($map)->count();
		$fields = array('qyx_publicno.publicnoid','qyx_cust.custid','qyx_cust.custtype','qyx_cust.nickname','qyx_cust.realname','qyx_cust.pinyinname','qyx_publicno.fundesc','qyx_publicno.accmbody');
	
		if(empty($data['sortField']))$data['sortField'] = "qyx_publicno.publicnoid";
		if(empty($data['sortOrder']))$data['sortOrder'] = "desc";
	
		if(empty($data['pageSize']) && empty($data['pageIndex']))
			$result = $this->join('join qyx_cust on qyx_publicno.custid = qyx_cust.custid')
							->join('join qyx_friend on qyx_publicno.custid = qyx_friend.tcustid')
							->field($fields)->where($map)->order("{$data['sortField']} {$data['sortOrder']}")->select();
		else
			$result = $this->join('join qyx_cust on qyx_publicno.custid = qyx_cust.custid')
							->join('join qyx_friend on qyx_publicno.custid = qyx_friend.tcustid')
							->field($fields)->where($map)->order("{$data['sortField']} {$data['sortOrder']}")->limit($data['pageIndex']*$data['pageSize'] , $data['pageSize'])->select();
	
		return array('total'=>$total , 'data'=>$result);
	}
	
	
	public function getPublicNoInfoById($publicnoid){
		$map['qyx_publicno.publicnoid'] = $publicnoid;
		$map['qyx_cust.custtype'] = array('in' , array(2,3));
		
		$fields = array('qyx_publicno.publicnoid','qyx_cust.custid','qyx_cust.custtype','qyx_cust.nickname','qyx_cust.realname','qyx_cust.pinyinname','qyx_cust.email',
						'qyx_publicno.fundesc','qyx_publicno.accmbody','qyx_publicno.cstel','qyx_publicno.menu',
						'qyx_cust_location.longitude','qyx_cust_location.latitude');
		$result = $this->join('join qyx_cust on qyx_publicno.custid=qyx_cust.custid')
						->join('left join qyx_cust_location on qyx_publicno.custid=qyx_cust_location.custid')
						->field($fields)->where($map)->find();
		if(empty($result)) $result = array();
		return $result;
	}
	
	
	public function getPublicNoInfoByCust($custid){
		$map['qyx_publicno.custid'] = $custid;
		$map['qyx_cust.custtype'] = array('in' , array(2,3));
		
		$fields = array('qyx_publicno.publicnoid','qyx_cust.custid','qyx_cust.custtype','qyx_cust.nickname','qyx_cust.nickname','qyx_cust.realname','qyx_cust.pinyinname',
				'qyx_publicno.fundesc','qyx_publicno.accmbody','qyx_publicno.cstel','qyx_publicno.menu',
				'qyx_cust_location.longitude','qyx_cust_location.latitude');
		$result = $this->join('join qyx_cust on qyx_publicno.custid=qyx_cust.custid')
						->join('left join qyx_cust_location on qyx_publicno.custid=qyx_cust_location.custid')
						->field($fields)->where($map)->find();
		if(empty($result)) $result = array();
		return $result;
	}
	
	
	public function getPublicNoBaseInfoById($publicnoid){
		$map['publicnoid'] = $publicnoid;
		$map['custtype'] = array('in' , array(2,3));
		
		$fields = array('publicnoid','fundesc','accmbody','cstel','menu');
		$result = $this->field($fields)->where($map)->find();
		if(empty($result)) $result = array();
		return $result;
	}
	
	public function getPublicNoBaseInfoByCust($custid){
		$map['custid'] = $custid;
		$map['custtype'] = array('in' , array(2,3));
	
		$fields = array('publicnoid','fundesc','accmbody','cstel','menu');
		$result = $this->field($fields)->where($map)->find();
		if(empty($result)) $result = array();
		return $result;
	}
	
	
	public function getPublicNoInfoByEmail($email){
		$map['qyx_cust.email'] = $email;
		$map['qyx_cust.custtype'] = array('in' , array(2,3));
		
		$fields = array('qyx_publicno.publicnoid','qyx_cust.custid','qyx_cust.email','qyx_cust.password','qyx_cust.custtype','qyx_cust.nickname','qyx_cust.realname','qyx_cust.pinyinname',
						'qyx_publicno.fundesc','qyx_publicno.accmbody','qyx_publicno.cstel','qyx_publicno.menu',
						'qyx_cust_location.longitude','qyx_cust_location.latitude');
		$result = $this->join('left join qyx_cust on qyx_publicno.custid=qyx_cust.custid')
						->join('left join qyx_cust_location on qyx_publicno.custid=qyx_cust_location.custid')
						->field($fields)->where($map)->find();
		if(empty($result)) $result = array();
		return $result;
	}
	
	public function addPublicNo($data){
		$result = $this->add($data);
		return $result;
	}
	
	public function updatePublicNo($data){
		$map['publicnoid'] = $data['publicnoid'];
		$data['updatetime'] = date("Y-m-d H:i:s");
		$result = $this->where($map)->save($data);
		return $result;
	}
	
}


